package com.rs.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.ZhUserMapper;
import com.rs.model.CommonResult;
import com.rs.model.ZhUser;
import com.rs.service.ZhUserTeacherService;


@Service
@Transactional
public class ZhUserTeacherServiceImpl implements ZhUserTeacherService {
	
	@Autowired
	ZhUserMapper zhUserMapper;
	
	static final Integer NUMBER=10;
	
	//将日期格式转换
	SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override  //添加一名教师
	public CommonResult<Integer> addTeacher(ZhUser user) {
		// TODO Auto-generated method stub
		/*if(user.getuPoid()!=2){  //判断添加是否为教师
			return new CommonResult<>(0, "添加选项非教师");
		}*/
		ZhUser zhUser = zhUserMapper.selectByPrimaryKey(user);
		if(zhUser != null){
			return new CommonResult<>(0, "账号已存在");
		}
		//user.setuDeid(1);
		//user.setuPoid(2);
		try {
			if(user.getuBirthdayString() !=null){	
					user.setuBirthday(sm.parse(user.getuBirthdayString()));
			}
			if(user.getuEntrytimeString() !=null){
				user.setuEntrytime(sm.parse(user.getuEntrytimeString()));
			}
			if(user.getuRetimeString() !=null){
				user.setuRetime(sm.parse(user.getuRetimeString()));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		user.setuExamine(0);
		int influence= zhUserMapper.insertSelective(user);
		CommonResult<Integer> comm=null;
		if(influence>0){
			comm=new CommonResult<>(200, "添加成功",influence);
		}else{
			comm=new CommonResult<>(0, "添加失败",influence);
		}
		return comm;
	}

	//修改一名教师信息
	@Override
	public CommonResult<Integer> updateTeacher(ZhUser user) {
		// TODO Auto-generated method stub
		/*if(user.getuPoid()!=2){  
		return new CommonResult<>(0, "修改选项非教师");
	}*/
		int influence= zhUserMapper.updateByPrimaryKeySelective(user);
		CommonResult<Integer> comm=null;
		if(influence>0){
			comm=new CommonResult<>(200, "修改成功",influence);
		}else{
			comm=new CommonResult<>(0, "修改失败",influence);
		}
		return comm;
	}

	//删除一条教师信息
	@Override
	public CommonResult<Integer> deleteTeacher(ZhUser user) {
		// TODO Auto-generated method stub
		/*if(user.getuPoid()!=2){  
		return new CommonResult<>(0, "删除选项非教师");
	}*/
		int influence= zhUserMapper.deleteByPrimaryKey(user.getuId());
		CommonResult<Integer> comm=null;
		if(influence>0){
			comm=new CommonResult<>(200, "删除成功",influence);
		}else{
			comm=new CommonResult<>(0, "删除失败",influence);
		}
		return comm;
	}

	//查询一条教师信息
	@Override
	public CommonResult<ZhUser> selectTeacher(ZhUser user) {
		// TODO Auto-generated method stub
				/*if(user.getuPoid()!=2){  
				return new CommonResult<>(0, "查询选项非教师");
			}*/
		ZhUser influence= zhUserMapper.selectByPrimaryKey(user);
		CommonResult<ZhUser> comm=null;
		if(influence!=null){
			//时间转换
			if(influence.getuBirthday() !=null){
				influence.setuBirthdayString(sm.format(influence.getuBirthday()));
			}
			if(influence.getuEntrytime() !=null){
				influence.setuEntrytimeString(sm.format(influence.getuEntrytime()));
			}
			if(influence.getuRetime() !=null){
				influence.setuRetimeString(sm.format(influence.getuRetime()));
			}
			comm=new CommonResult<>(200, "成功",influence);
		}else{
			comm=new CommonResult<>(0, "失败",influence);
		}
		return comm;
	}
 
	//查询所有老师
	@Override
	public Page<ZhUser> selectTeachers(ZhUser user) {
		// TODO Auto-generated method stub
		PageHelper.startPage(user.getPageNum(),NUMBER);
		Page<ZhUser> lsit=zhUserMapper.selectUsers(user);
		for(ZhUser influence:lsit.getResult()){
			//时间转换
			if(influence.getuBirthday() !=null){
				influence.setuBirthdayString(sm.format(influence.getuBirthday()));
			}
			if(influence.getuEntrytime() !=null){
				influence.setuEntrytimeString(sm.format(influence.getuEntrytime()));
			}
			if(influence.getuRetime() !=null){
				influence.setuRetimeString(sm.format(influence.getuRetime()));
			}
		}
		return lsit;
	}

	@Override
	public List<ZhUser> listTeacher(ZhUser user) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
	

}
